package com.lufei4.chestnut.view;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.lufei4.chestnut.Constans;
import com.lufei4.chestnut.R;
import com.lufei4.chestnut.net2.ApiUrl;
import com.lufei4.chestnut.net2.BaseObserver;
import com.lufei4.chestnut.net2.BaseResponse;
import com.lufei4.chestnut.net2.MyObserver;
import com.lufei4.chestnut.net2.RxExceptionUtil;
import com.lufei4.chestnut.net2.RxHelper;
import com.lufei4.chestnut.network.WxarticleSBean;
import com.lufei4.chestnut.network.WxarticleSBean2;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.RxActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class TwoActivity extends RxActivity {
    @BindView(R.id.editTextTextPersonName)
    EditText editTextTextPersonName;
    @BindView(R.id.editTextTextPassword)
    EditText editTextTextPassword;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.content)
    TextView content;

    //'Retrofit(okhttp3.Call.Factory, okhttp3.HttpUrl, java.util.List<retrofit2.Converter.Factory>, java.util.List<retrofit2.CallAdapter.Factory>, java.util.concurrent.Executor, boolean)' is not public in 'retrofit2.Retrofit'. Cannot be accessed from outside package
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private Retrofit retrofit;

    private void initView() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Constans.BaseUrl)
                .client(GetOkHttpClient.getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    @OnClick({R.id.button, R.id.button2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button:
                getNet();
                break;
            case R.id.button2:
                getNet4();
                break;
        }
    }

    String TAG = "lufeijjj";

    private void getNet() {


        ApiUrl api = retrofit.create(ApiUrl.class);
        Call<WxarticleSBean> demo = api.getRetrofit();
        demo.enqueue(new Callback<WxarticleSBean>() {
            @Override
            public void onResponse(Call<WxarticleSBean> call, Response<WxarticleSBean> response) {
                Log.e(TAG, "请求成功信息: " + response.body().toString());
                content.setText(response.body().data.toString());
            }

            @Override
            public void onFailure(Call<WxarticleSBean> call, Throwable t) {

            }
        });
    }

    private void getNet2() {

        retrofit.create(ApiUrl.class)
                .getRetrofit1()
                .subscribeOn(Schedulers.io())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WxarticleSBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.e(TAG, "onSubscribe: ");

                    }

                    @Override
                    public void onNext(WxarticleSBean wxarticleSBean) {
                        Log.e(TAG, "onNext: " + wxarticleSBean.toString());

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "Throwable: " + e.getMessage());

                    }

                    @Override
                    public void onComplete() {
                        Log.e(TAG, "onComplete: ");

                    }
                });
    }

    private void getNet3() {
        retrofit.create(ApiUrl.class)
                .getRetrofit2()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Observer<BaseResponse<WxarticleSBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<WxarticleSBean> wxarticleSBeanBaseResponse) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getNet4() {
        retrofit.create(ApiUrl.class)
                .getRetrofit4()
                .compose(RxHelper.observableIO2Main(this))
                .subscribe(new MyObserver<List<WxarticleSBean2>>(this) {
                    @Override
                    public void onSuccess(List<WxarticleSBean2> demo) {
                        content.setText(demo.toString());
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {

                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        onFailure(e, RxExceptionUtil.exceptionHandler(e));

                    }
                });

    }


}